package com.collect.zambia.zhiScene.zhiTelemarketing.zhiRoot;

import android.annotation.SuppressLint;
import android.util.Log;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.zambia.R;
import com.collect.zambia.zhiBase.BaseActivity;
import com.collect.zambia.zhiBean.zhiSend.ZHISTelemarketingFollowListEntity;
import com.collect.zambia.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.zambia.zhiScene.zhiTelemarketing.zhiAccount.ZHITelemaketingAccountFragment;
import com.collect.zambia.zhiScene.zhiTelemarketing.zhiFollow.ZHITelemarketingFollowFragment;
import com.collect.zambia.zhiScene.zhiTelemarketing.zhiLogic.ZHITelemarketingFollowHelper;
import com.collect.zambia.zhiScene.zhiTelemarketing.zhiPerformance.ZHIPerformanceTelemarketingFragment;
import com.collect.zambia.zhiScene.zhiWidget.zhiFilterDaily.ZHIFilterDailyClickListener;
import com.collect.zambia.zhiScene.zhiWidget.zhiFilterTelemarketing.ZHIFilterTelemaketing;
import com.collect.zambia.zhiScene.zhiWidget.zhiNavigationBar.ZHIBottomTabBarItem;
import com.collect.zambia.zhiUtils.ZHICalendarUtil;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_ROOT_TELEMARKETING)
public class ZHIRootTelemaketingActivity extends BaseActivity implements ZHIRootTelemaketingFragmentInterface {

    @BindView(R.id.drawer_layout)
    DrawerLayout drawer_layout;

    @BindView(R.id.drawer_filter_telemarketing)
    ZHIFilterTelemaketing drawer_filter_telemarketing;

    @BindView(R.id.bar_loan)
    ZHIBottomTabBarItem bar_loan;

    @BindView(R.id.bar_marked)
    ZHIBottomTabBarItem bar_marked;

    @BindView(R.id.bar_account)
    ZHIBottomTabBarItem bar_account;

    private FragmentManager mFragmentManager;
    private ZHITelemarketingFollowFragment mZHITelemarketingFollowFragment;
    private ZHITelemaketingAccountFragment mZHITelemaketingAccountFragment;
    private ZHIPerformanceTelemarketingFragment mZHIPerformanceTelemarketingFragment;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_root_telemarketing;
    }

    @Override
    protected void initInjector() {
//        mActivityComponent.inject(this);
    }

    @Override
    protected void initView() {
        mFragmentManager = getSupportFragmentManager();

        drawer_filter_telemarketing.setZHIFilterDailyClickListener(new ZHIFilterDailyClickListener() {
            @Override
            public void onConfirmClick() {
                if (drawer_layout.isDrawerOpen(drawer_filter_telemarketing)) {
                    drawer_layout.closeDrawer(drawer_filter_telemarketing);
                }
                if (mZHITelemarketingFollowFragment != null) {
                    mZHITelemarketingFollowFragment.refreshData();
                }
            }

            @Override
            public void onCloseClick() {
                if (drawer_layout.isDrawerOpen(drawer_filter_telemarketing)) {
                    drawer_layout.closeDrawer(drawer_filter_telemarketing);
                }
            }
        });


        initEvent();
        selecFragment(0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: ============");

        //如果在跟进，刷新状态
        ZHITelemarketingFollowHelper.getInstance().refershFollowCache();
    }

    @Override
    public void onShowFilterFollow() {
        if (drawer_layout.isDrawerOpen(drawer_filter_telemarketing)) {
            drawer_layout.closeDrawer(drawer_filter_telemarketing);
        } else {
            drawer_layout.openDrawer(drawer_filter_telemarketing);
        }
    }

    @Override
    public ZHISTelemarketingFollowListEntity onGetFollowFilterParams() {
        ZHISTelemarketingFollowListEntity zhisTelemarketingFollowListEntity = new ZHISTelemarketingFollowListEntity();
        //默认传空串,GOOD_CLIENT,NOT_RELOAN,NOT_APPLY
        zhisTelemarketingFollowListEntity.setClientType(drawer_filter_telemarketing.getClient());
        //默认UNFOLLOWED,FOLLOWED,FOCUS_ON
        zhisTelemarketingFollowListEntity.setFollowStatus(drawer_filter_telemarketing.getStatus());
        zhisTelemarketingFollowListEntity.setFollowFlag(drawer_filter_telemarketing.getOrderType());
        zhisTelemarketingFollowListEntity.setMobile(drawer_filter_telemarketing.getUserMobile());
        zhisTelemarketingFollowListEntity.setAssignStartDate(drawer_filter_telemarketing.getAssignStartDate());
        zhisTelemarketingFollowListEntity.setAssignEndDate(ZHICalendarUtil.getDateStringAfterDate(drawer_filter_telemarketing.getAssignEndDate(), 1));
        return zhisTelemarketingFollowListEntity;
    }

    public void initEvent() {
        bar_loan.setCFOnMenuItemDidChangedListener(item -> {
            bar_marked.setChecked(false);
            bar_account.setChecked(false);
            selecFragment(0);
        });
        bar_marked.setCFOnMenuItemDidChangedListener(item -> {
            bar_loan.setChecked(false);
            bar_account.setChecked(false);
            selecFragment(1);
        });
        bar_account.setCFOnMenuItemDidChangedListener(item -> {
            bar_loan.setChecked(false);
            bar_marked.setChecked(false);
            selecFragment(2);
        });
    }

    private void selecFragment(int indext) {
        FragmentTransaction fragemntTransaction = mFragmentManager.beginTransaction();
        hideFragment(fragemntTransaction);
        switch (indext) {
            case 0: {
                bar_loan.setChecked(true);
                bar_marked.setChecked(false);
                bar_account.setChecked(false);
                if (null == mZHITelemarketingFollowFragment) {
                    mZHITelemarketingFollowFragment = new ZHITelemarketingFollowFragment();
                    fragemntTransaction.add(R.id.content, mZHITelemarketingFollowFragment);
                } else {
                    fragemntTransaction.show(mZHITelemarketingFollowFragment);
                }
                break;
            }
            case 1: {
                bar_loan.setChecked(false);
                bar_marked.setChecked(true);
                bar_account.setChecked(false);
                if (null == mZHIPerformanceTelemarketingFragment) {
                    mZHIPerformanceTelemarketingFragment = new ZHIPerformanceTelemarketingFragment();
                    fragemntTransaction.add(R.id.content, mZHIPerformanceTelemarketingFragment);
                } else {
                    fragemntTransaction.show(mZHIPerformanceTelemarketingFragment);
                }
                break;
            }
            case 2: {
                bar_loan.setChecked(false);
                bar_marked.setChecked(false);
                bar_account.setChecked(true);
                if (null == mZHITelemaketingAccountFragment) {
                    mZHITelemaketingAccountFragment = new ZHITelemaketingAccountFragment();
                    fragemntTransaction.add(R.id.content, mZHITelemaketingAccountFragment);
                } else {
                    fragemntTransaction.show(mZHITelemaketingAccountFragment);
                }
                break;
            }
            default:
                break;
        }
        fragemntTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragemntTransaction) {
        if (fragemntTransaction == null) return;
        if (null != mZHITelemarketingFollowFragment) {
            fragemntTransaction.hide(mZHITelemarketingFollowFragment);
        }
        if (null != mZHITelemaketingAccountFragment) {
            fragemntTransaction.hide(mZHITelemaketingAccountFragment);
        }
        if (null != mZHIPerformanceTelemarketingFragment) {
            fragemntTransaction.hide(mZHIPerformanceTelemarketingFragment);
        }
    }
}
